function [MAG,PHASE] = sobel(M_ORG)

FLT_SOBEL = [-1 0 1; -2 0 2; -1 0 1];
FLT_SOBEL_90 = rot90(FLT_SOBEL);

M_SOBEL = conv2(M_ORG,FLT_SOBEL,'same');
M_SOBEL_90 = conv2(M_ORG,FLT_SOBEL_90, 'same');

[height,width] = size(M_ORG);

for i=1:height
	for j=1:width
		if ( (i == 1) | (j == 1) | (i == height) | (j == width))
			MAG(i,j) = 0;
			PHASE(i,j) = 0;
		else
			MAG(i,j) = ((abs(M_SOBEL(i,j)))^2 + (abs(M_SOBEL_90(i,j)))^2)^0.5;
			if (M_SOBEL(i,j) == 0)
				if (M_SOBEL_90(i,j) ~= 0)
					PHASE(i,j) = -90;
				else
					PHASE(i,j) = 0;
				end
			else
				PHASE(i,j) = 180/pi*atan(M_SOBEL_90(i,j)/M_SOBEL(i,j));
			end
		end

		p = PHASE(i,j);
		if ( (p >= -22.5) & (p < 22.5))
			PHASE(i,j) = 0 ;
		elseif ((p >= 22.5) & (p < 67.5))
			PHASE(i,j) = 45;
		elseif ((p >= -67.4) & (p < -22.5))
			PHASE(i,j) = 135;
		else
			PHASE(i,j) = 90;
		end
	end
end

max_intensity = max(max(MAG));
MAG = MAG ./ max_intensity;
